﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.CLASS;
using WindowsFormsApplication2.All_FORMS.Bvrtgel_forms;

namespace WindowsFormsApplication2.All_FORMS.Lavlah_forms
{
    public partial class frmWorker : Form
    {
        public frmWorker()
        {
            InitializeComponent();
        }

        private void btnRegAdd_Click(object sender, EventArgs e)
        {
            frmRegWorker frm = new frmRegWorker();
            frm.Show();
            
        }

        private void frmWorker_Load(object sender, EventArgs e)
        {
            dataGridViewWorker.DataSource = DBTools.selecQryTmp("Select *from Worker");
            FormatGrid();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridViewWorker.DataSource = DBTools.selecQryTmp("Select *from Worker");

        }
        public void FormatColumn(String colname, String caption, int index)
        {
            dataGridViewWorker.Columns[colname].HeaderText = caption;
            dataGridViewWorker.Columns[colname].DisplayIndex = index;
            dataGridViewWorker.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        public void FormatGrid()
        {
            dataGridViewWorker.AllowUserToOrderColumns = true;
            dataGridViewWorker.AllowUserToAddRows = false;
            dataGridViewWorker.AllowUserToDeleteRows = false;
            dataGridViewWorker.ReadOnly = true;
            dataGridViewWorker.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewWorker.MultiSelect = true;

            FormatColumn("WorkerId", "Ажилтны код", 0);
            FormatColumn("WorkerFirstName", "Ажилтны овог", 1);
            FormatColumn("WorkerLasName", "Ажилтны нэр", 2);
            FormatColumn("WorkerRd", "Регистрийн дугаар", 3);
            FormatColumn("WorkerAddress", "Ажилтны хаяг", 4);
            FormatColumn("WorkerPhone", "Ажилтны утас", 5);
            FormatColumn("WorkerType", "Ажилтны төрөл", 5);
            dataGridViewWorker.Columns["WorkerPass"].Visible = false;
        }
        private void btnRegEdit_Click(object sender, EventArgs e)
        {
            
        }

        private void btnRegDel_Click(object sender, EventArgs e)
        {
            int i = 0;
            Worker cls = new Worker();

            i = dataGridViewWorker.CurrentRow.Index;
            DataRowView drv = dataGridViewWorker.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string byWorkerId = drv[0].ToString();

                Worker.DelFunction(byWorkerId);
                MessageBox.Show("Амжилттай устгалаа");
                dataGridViewWorker.DataSource = DBTools.selecQryTmp("Select *from Worker");

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridViewWorker_DoubleClick(object sender, EventArgs e)
        {
            int i = 0;
            Worker cls1 = new Worker();

            i = dataGridViewWorker.CurrentRow.Index;
            DataRowView drv = dataGridViewWorker.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string byWorkerId = drv[0].ToString();

                cls1 = Worker.getbyRec(byWorkerId);
                Form EA = new frmRegWorker(cls1);
                EA.ShowDialog();
                dataGridViewWorker.DataSource = DBTools.selecQryTmp("Select *from Worker");
            }
        }
    }
}
